package v9.assist.analyst

import android.graphics.Bitmap
import kotlinx.coroutines.script.Block
import kotlinx.coroutines.script.TARGET
import kotlinx.coroutines.script.analysis.Analysis
import kotlinx.coroutines.script.SMapBase.hitDeathRed
import kotlinx.coroutines.script.SMapBase.hitEquipO
import kotlinx.coroutines.script.SMapBase.hitSMapEnemyBlue
import kotlinx.coroutines.script.SMapBase.hitSMapEnemyYellow
import kotlinx.coroutines.script.SMapBase.hitTeammate
import kotlinx.coroutines.script.tes.ColorDiffer
import kotlinx.coroutines.script.tes.printLog
import kotlinx.coroutines.script.tes.pt
import kotlinx.coroutines.script.tes.rgb
import kotlinx.coroutines.script.tes.shake
import v9.assist.script.xBossBesideTs
import v9.assist.script.xLastAtkTs
import v9.assist.script.xLastPetBlacksmithTs
import v9.assist.script.xLastSceneEnterTs
import v9.assist.script.xLastUnknownTs
import kotlin.math.abs

private val hitRoad by lazy { // 21-28
    val out = mutableSetOf<ColorDiffer>()
    out.addAll(setOf(0xff393830.toInt().rgb(12),)) // p1
    out.addAll(setOf(0xff38362d.toInt().rgb(12),)) // p2
    out.addAll(setOf(0xff3a352a.toInt().rgb(12),)) // p2 船起点
    out.addAll(setOf(0xff3f3f38.toInt().rgb(12),)) // p3
    out.addAll(hitEquipO)
    out.addAll(hitDeathRed)
    out.addAll(hitTeammate)
    out.addAll(hitSMapEnemyYellow)
    out.addAll(hitSMapEnemyBlue)
    out
}
private val cP1StartPoint1 by lazy {  Block("小船检测", selfInSmallMap.x + 10.pt, selfInSmallMap.y + 40.pt, 40.pt, 30.pt) }
private val cP2StartA1 by lazy {  Block("暴风船内A检测1", selfInSmallMap.x - 150.pt, selfInSmallMap.y - 100.pt, 20.pt, 60.pt) }
private val cP2StartA2 by lazy {  Block("暴风船内A检测2", selfInSmallMap.x - 130.pt, selfInSmallMap.y - 40.pt, 40.pt, 20.pt) }
private val cP2StartA3 by lazy {  Block("暴风船内楼梯A检测3", selfInSmallMap.x - 50.pt, selfInSmallMap.y + 0.pt, 20.pt, 20.pt) }
private val cP2StartA4 by lazy {  Block("暴风船内楼梯A检测4", selfInSmallMap.x + 20.pt, selfInSmallMap.y + 20.pt, 20.pt, 30.pt) }
private val cP2StartA5 by lazy {  Block("暴风船外A检测5反", selfInSmallMap.x + 30.pt, selfInSmallMap.y - 20.pt, 20.pt, 20.pt) }
private val cP2StartA6 by lazy {  Block("暴风船外A检测6反", selfInSmallMap.x - 30.pt, selfInSmallMap.y - 40.pt, 20.pt, 20.pt) }
private val cP2StartA7 by lazy {  Block("暴风船内A检测7反", selfInSmallMap.x - 40.pt, selfInSmallMap.y - 100.pt, 20.pt, 70.pt) }
private val cP2StartA8 by lazy {  Block("暴风船内A检测8反", selfInSmallMap.x - 20.pt, selfInSmallMap.y - 60.pt, 40.pt, 20.pt) }
private val cP2StartA9 by lazy {  Block("暴风船内A检测9反", selfInSmallMap.x - 100.pt, selfInSmallMap.y + 30.pt, 20.pt, 20.pt) }
private val cP2StartB1 by lazy {  Block("暴风船外B检测1", selfInSmallMap.x - 40.pt, selfInSmallMap.y + 10.pt, 20.pt, 20.pt) }
private val cP2StartB2 by lazy {  Block("暴风船外B检测2", selfInSmallMap.x + 20.pt, selfInSmallMap.y + 10.pt, 20.pt, 20.pt) }
private val cP2StartBX by lazy {  Block("暴风船外B检测X", selfInSmallMap.x - 140.pt, selfInSmallMap.y - 50.pt, 20.pt, 20.pt) }
private val cP2StartB3 by lazy {  Block("暴风船内B检测3", selfInSmallMap.x - 50.pt, selfInSmallMap.y - 58.pt, 20.pt, 20.pt) }
private val cP2StartB4 by lazy {  Block("暴风船内B检测4", selfInSmallMap.x - 50.pt, selfInSmallMap.y - 78.pt, 20.pt, 20.pt) }
private val cP2StartB5 by lazy {  Block("暴风船内B检测5", selfInSmallMap.x - 0.pt, selfInSmallMap.y - 88.pt, 20.pt, 20.pt) }
private val cP2StartB6 by lazy {  Block("暴风船内B检测6", selfInSmallMap.x + 20.pt, selfInSmallMap.y - 66.pt, 20.pt, 20.pt) }
private val cP2StartC1 by lazy {  Block("暴风船内C检测1", selfInSmallMap.x - 115.pt, selfInSmallMap.y - 40.pt, 40.pt, 20.pt) }
private val cP2StartC2 by lazy {  Block("暴风船内C检测2", selfInSmallMap.x - 55.pt, selfInSmallMap.y - 20.pt, 20.pt, 20.pt) }
private val cP2StartC3 by lazy {  Block("暴风船内C检测3", selfInSmallMap.x - 45.pt, selfInSmallMap.y - 42.pt, 20.pt, 20.pt) }
private val cP2StartC4 by lazy {  Block("暴风船内C检测4", selfInSmallMap.x - 5.pt, selfInSmallMap.y - 40.pt, 20.pt, 20.pt) }
private val cP2StartC5 by lazy {  Block("暴风船内C检测5", selfInSmallMap.x - 90.pt, selfInSmallMap.y - 80.pt, 30.pt, 20.pt) }
private val hitBlack by lazy { setOf(0xff0b1111.toInt().rgb(6),0xff101919.toInt().rgb(5),) }
private val cSurroundBlack0 by lazy { Block("角色周边1", selfInSmallMap.x - 10.pt, selfInSmallMap.y - 30.pt, 20.pt, 20.pt) }
private val cSurroundBlack45 by lazy { Block("角色周边2", selfInSmallMap.x + 10.pt, selfInSmallMap.y - 30.pt, 20.pt, 20.pt) }
private val cSurroundBlack90 by lazy { Block("角色周边3", selfInSmallMap.x + 10.pt, selfInSmallMap.y - 10.pt, 20.pt, 20.pt) }
private val cSurroundBlack135 by lazy { Block("角色周边4", selfInSmallMap.x + 10.pt, selfInSmallMap.y + 10.pt, 20.pt, 20.pt) }
private val cSurroundBlack180 by lazy { Block("角色周边5", selfInSmallMap.x - 10.pt, selfInSmallMap.y + 10.pt, 20.pt, 20.pt) }
private val cSurroundBlack225 by lazy { Block("角色周边6", selfInSmallMap.x - 30.pt, selfInSmallMap.y + 10.pt, 20.pt, 20.pt) }
private val cSurroundBlack270 by lazy { Block("角色周边7", selfInSmallMap.x - 30.pt, selfInSmallMap.y - 10.pt, 20.pt, 20.pt) }
private val cSurroundBlack315 by lazy { Block("角色周边8", selfInSmallMap.x - 30.pt, selfInSmallMap.y - 30.pt, 20.pt, 20.pt) }
var anaLastSMapFBTargetSampleRightTs = 0L
//var anaSurroundBlackTs: Long = 0L // 在沉船里
var anaLastSMapFBFeature1Ts = 0L //
var anaLastSMapFBFeature3Ts = 0L // 周围没有路
var anaLastSMapFBFeature4Ts = 0L // 起点小船
var anaLastSMapFBFeature7Ts = 0L // 直通特征
var anaLastSMapFBFeature8Ts = 0L // 副本外
var anaLastSMapFBFeature9Ts = 0L // 大船甲板右侧
var anaLastSMapFBFeature10Ts = 0L // 遇难的船长
var anaLastSMapFBFeature11Ts = 0L // 调整去大船
var anaLastSMapFBFeature13Ts = 0L // 副本起点
var anaLastSMapFBFeature14Ts = 0L // 卡笼子

fun rasterizeMapFB(ss: Bitmap): Array<IntArray> {
    return rasterizeMap(ss, hitRoad)
}

fun analysisMapFB(ss: Bitmap) {
//    printLog { "analysisMapFB" }
    analysisMapElements(ss)
    val returnMapArray = rasterizeMapFB(ss)

    if (blockEliteEnemyBoss != null && mapEnemyList.isNotEmpty()) {
        // 优先Boss
        mapTargetList.add(mapEnemyList.random())
        printLog { "analyst target to boss" }
        lastSMapTargetToBossTs = System.currentTimeMillis()
        if (mapElementDistance(blockEliteEnemyBoss) < 30.pt) xBossBesideTs = System.currentTimeMillis()
    } else if (bsSceneSettleBlacksmith && mapBlacksmithList.isNotEmpty()) {
        mapTargetList.add(mapBlacksmithList.random())
        printLog { "analyst target to blacksmith" }
    } else if (bsSceneSettle && mapBlueDoorList.isNotEmpty()) {
        mapTargetList.add(mapBlueDoorList.random())
        printLog { "analyst target to blueDoor" }
    } else if (mapRightNotArrived.size > 0) {
        mapTargetList.add(mapRightNotArrived.random())
    } else if (mapSampleArrived.size > 0) {
        if (mapEnemyList.isNotEmpty()) {
            mapTargetList.add(mapEnemyList.random())
            printLog { "analyst target to enemy in sample" }
        } else if (System.currentTimeMillis() - anaLastHasEnemyBossNameTs < 5000) { // 无boss图标的带名字
            val d = (System.currentTimeMillis() / 3000) % 2
            if (d == 0L) {
                mapSampleArrived.filter {
                    it.second < selfInSmallMap.y - smallMap.top + 30.pt
                        && (it.first > selfInSmallMap.x - smallMap.left + 10.pt)
                }
                    .minByOrNull { it.second }?.also {
                        mapTargetList.add(it)
                        printLog { "analyst target to sample right top" }
                    }
            } else {
                mapSampleArrived.filter {
                    it.second < selfInSmallMap.y - smallMap.top
                }
                    .minByOrNull { it.second }?.also {
                        mapTargetList.add(it)
                        printLog { "analyst target to sample top" }
                    }
            }
        } else if (System.currentTimeMillis() - lastEliteEnemyBossTs < 6000) {
            mapSampleArrived.filter { it.first < selfInSmallMap.x - smallMap.left }
                .minByOrNull { it.first * 2 + it.second }?.also {
                    mapTargetList.add(it)
                    printLog { "analyst target to sample left top" }
                }
        } else {
            mapSampleArrived.filter { it.first > selfInSmallMap.x - smallMap.left }
                .maxByOrNull { it.first }?.also {
                    mapTargetList.add(it)
                    printLog { "analyst target to sample right" }
                    anaLastSMapFBTargetSampleRightTs = System.currentTimeMillis()
                }
        }

    }

    if (mapTargetList.isNotEmpty()) {
        val target = mapTargetList.random()
        returnMapArray[target.first][target.second] = TARGET
    }

    angleByRoad = analysisRasterizeMap(returnMapArray)
    angleByRoad = angleSpecialAdjust(angleByRoad ?: 75.shake(10))

    // map feature
    analysisMapFBIsStartPoint(ss)

    if ((blockBlueDoor == null || blockBlacksmith == null)
        && System.currentTimeMillis() - lastEliteEnemyBossTs > 10000) {
         printLog { "analystFB $mapSimpleStatus" }

        if (System.currentTimeMillis() - lastMapTopNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapBottomNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapLeftNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapRightNotArrivedTs > 2000
            && lastMapTopP == null
            && lastMapBottomP == null
            && lastMapLeftP == null
            && lastMapRightP == null
            && lastMapRoadC == 0
            && System.currentTimeMillis() - xLastUnknownTs > 8000) {
            printLog { "analystFB 节点.周围无路" }
            anaLastSMapFBFeature3Ts = System.currentTimeMillis()
        } else if (System.currentTimeMillis() - lastMapTopNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapBottomNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapLeftNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapRightNotArrivedTs > 2000
            && lastMapTopP == null
            && (lastMapBottomP ?: -1f) in 0.48 .. 0.58
            && lastMapLeftP == null
            && lastMapRightP == null
            && System.currentTimeMillis() - anaLastHasEnemyBossNameTs > 20000
            && System.currentTimeMillis() - anaLastSMapFBFeature4Ts > 10000
            && lastMapRoadC in 4 .. 20
            && System.currentTimeMillis() - anaLastSMapFBFeature10Ts > 6000
        ) {
            // [上,1739453197s,c:0][下,0s,0.53060263,c:24][左,1739453197s,c:0][右,1739453197s,c:0]][Road,c:9]
            // [上,1739453222s,c:0][下,0s,0.5316384,c:15][左,1739453222s,c:0][右,1739453222s,c:0]][Road,c:7]
            printLog { "analystFB 节点.直通" }
            anaLastSMapFBFeature7Ts = System.currentTimeMillis()
        } else if (System.currentTimeMillis() - lastMapTopNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapBottomNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapLeftNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapRightNotArrivedTs > 2000
            && lastMapTopP == null
            && (lastMapBottomP ?: -1f) in 0.44 .. 0.68
            && lastMapLeftP == null
            && lastMapRightP == null
            && System.currentTimeMillis() - anaLastHasEnemyBossNameTs < 1000
            && System.currentTimeMillis() - lastEliteEnemyBossTs > 2000
            && System.currentTimeMillis() - anaLastSMapFBFeature4Ts > 12000
        ) {
            printLog { "analystFB 节点.卡笼子" }
            // [上,200s,c:0][下,0s,0.5528415,c:34][左,239s,c:0][右,205s,c:0]][Road,c:16]
            anaLastSMapFBFeature14Ts = System.currentTimeMillis()
        } else if (System.currentTimeMillis() - lastMapBottomNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapLeftNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapRightNotArrivedTs > 2000
            && (lastMapTopP == null || (lastMapTopP ?: -1f) in 0.01 .. 0.09)
            && lastMapBottomP == null
            && (lastMapLeftP ?: -1f) in 0.15 .. 0.36
            && lastMapRightP == null
            && System.currentTimeMillis() - anaLastSMapFBFeature1Ts > 12000
            && System.currentTimeMillis() - anaLastSMapFBFeature9Ts > 12000
            && lastMapRoadC in 6 .. 24
        ) {
            // [上,25s,c:0][下,12s,c:0][左,0s,0.20889895,c:34][右,1739369690s,c:0]][Road,c:12]
            // [上,0s,0.032485876,c:8][下,52s,c:0][左,0s,0.21462488,c:36][右,1739369731s,c:0]][Road,c:13]
            // [上,7s,c:0][下,98s,c:0][左,0s,0.23554549,c:34][右,1739369777s,c:0]][Road,c:12]
            // [上,0s,0.05323772,c:13][下,1739369988s,c:0][左,0s,0.3233618,c:30][右,1739369988s,c:0]][Road,c:13]
            // 非 用roadCount区别 [上,0s,0.37480223,c:50][下,9s,c:0][左,0s,0.3263608,c:19][右,1739453930s,c:0]][Road,c:31]
            printLog { "analystFB 节点.副本外入口" }
            anaLastSMapFBFeature8Ts = System.currentTimeMillis()
            anaClearSceneSettle()
        } else if (System.currentTimeMillis() - lastMapTopNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapLeftNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapRightNotArrivedTs > 2000
            && (lastMapTopP ?: -1f) in 0.08 .. 0.24
            && (lastMapBottomP == null || (lastMapBottomP ?: -1f) in 0.01 .. 0.10)
            && ((lastMapLeftP ?: -1f) in 0.48 .. 0.68 || (lastMapLeftP ?: -1f) in 0.07 .. 0.17)
            && lastMapRightP == null
            && System.currentTimeMillis() - anaLastSMapFBFeature1Ts > 30000
            && System.currentTimeMillis() - anaLastHasEnemyBossNameTs > 16000
        ) {
            // [上,0s,0.13653484,c:30][下,76s,c:0][左,0s,0.5039886,c:30][右,101s,c:0]][Road,c:27]
            // [上,0s,0.15294592,c:28][下,93s,c:0][左,0s,0.6620879,c:56][右,119s,c:0]][Road,c:30]
            // [上,0s,0.19774011,c:36][下,118s,c:0][左,0s,0.5315171,c:32][右,144s,c:0]][Road,c:27]
            // [上,0s,0.19365977,c:27][下,0s,0.072359845,c:13][左,0s,0.60438263,c:47][右,169s,c:0]][Road,c:31]
            // [上,0s,0.15850596,c:27][下,0s,0.017890772,c:3][左,0s,0.66855496,c:43][右,194s,c:0]][Road,c:30]
            // [上,0s,0.1680791,c:26][下,21s,c:0][左,0s,0.1011396,c:6][右,18s,c:0]][Road,c:34]
            // [上,0s,0.17175141,c:25][下,21s,c:0][左,0s,0.1011396,c:6][右,1743293108s,c:0]][Road,c:32]
            // PC 720P
            // [上,0s,0.17216772,c:19][下,24s,c:0][左,0s,0.13141026,c:4][右,27s,c:0]][Road,c:13]
            // [上,0s,0.18594219,c:17][下,56s,c:0][左,0s,0.16666667,c:5][右,60s,c:0]][Road,c:14]
            printLog { "analystFB 节点.大船甲板右侧" }
            anaLastSMapFBFeature9Ts = System.currentTimeMillis()
        } else if (System.currentTimeMillis() - lastMapTopNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapBottomNotArrivedTs > 8000
            && System.currentTimeMillis() - lastMapLeftNotArrivedTs > 8000
            && System.currentTimeMillis() - lastMapRightNotArrivedTs > 8000
            && (lastMapTopP ?: -1f) in 0.28 .. 0.42
            && lastMapBottomP == null
            && lastMapLeftP == null
            && lastMapRightP == null
            && System.currentTimeMillis() - anaLastPageSkipTs > 20000
            && System.currentTimeMillis() - anaLastSMapFBFeature3Ts > 6000
            && System.currentTimeMillis() - anaLastSMapFBFeature1Ts > 18000
        ) {
            // [上,0s,0.37867975,c:19][下,38s,c:0][左,1739370735s,c:0][右,1739370735s,c:0]][Road,c:11]
            printLog { "analystFB 节点.起点小船" }
            anaLastSMapFBFeature4Ts = System.currentTimeMillis()
            anaClearSceneSettle()
        } else if (System.currentTimeMillis() - lastMapTopNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapBottomNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapLeftNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapRightNotArrivedTs > 2000
            && lastMapTopP == null
            && (lastMapBottomP ?: -1f) in 0.22 .. 0.34
            && lastMapBottomC in 6 .. 20
            && lastMapLeftP == null
            && lastMapRightP == null
            && System.currentTimeMillis() - anaLastHasEnemyBossNameTs > 30000 // 有时候墨迹不出铁匠分析成功
            && !bsSceneSettle && !bsSceneSettleBlacksmith
        ) {
            // [上,17s,c:0][下,0s,0.2998386,c:14][左,142s,c:0][右,110s,c:0]][Road,c:21]
            // [上,49s,c:0][下,0s,0.26836157,c:14][左,174s,c:0][右,143s,c:0]][Road,c:22]
            // [上,24s,c:0][下,0s,0.29127434,c:9][左,130s,c:0][右,112s,c:0]][Road,c:28]
            printLog { "analystFB 节点.遇难的船长" }
            anaLastSMapFBFeature10Ts = System.currentTimeMillis()
        }
    }

    if (blockBlueDoor == null && blockBlacksmith == null) {
        if (System.currentTimeMillis() - lastMapTopNotArrivedTs < 1000
            && System.currentTimeMillis() - lastMapBottomNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapLeftNotArrivedTs > 2000
            && (lastMapTopP ?: -1f) in 0.62 .. 0.74
            && lastMapTopC in 30 .. 50
            && lastMapBottomP == null
            && lastMapLeftP == null
            && System.currentTimeMillis() - lastEliteEnemyBossTs > 2000
            && System.currentTimeMillis() - lastSMapTargetToBossTs > 2000
            && !bsSceneSettle && !bsSceneSettleBlacksmith
        ) {
            // [上,0s,0.6810807,c:39][下,18s,c:0][左,31s,c:0][右,0s,0.4008547,c:5]][Road,c:37]
            // [上,0s,0.6850955,c:42][下,20s,c:0][左,34s,c:0][右,2s,c:0]][Road,c:40]
            printLog { "analystFB 节点.调整去大船" }
            anaLastSMapFBFeature11Ts = System.currentTimeMillis()
        }

        if (System.currentTimeMillis() - anaLastSceneSettleTs > 10000 // 刚通关重叠错误判断
            && System.currentTimeMillis() - anaLastHasEnemyBossNameTs > 6000 // 在打boss还弹最终幕
            && System.currentTimeMillis() - lastEliteEnemyBossTs > 6000) { // 才打完boss图标.不会有这个
            if (System.currentTimeMillis() - lastMapRightNotArrivedTs > 2000
                && System.currentTimeMillis() - lastMapBottomNotArrivedTs > 2000
                && !Analysis.hit(ss, cP2StartA6, hitRoad, 4.pt, 0.1f)) { // 0
                var hitPart = 0
                if (Analysis.hit(ss, cP2StartA1, hitRoad, 4.pt, 0.55f)) hitPart++ // , "p2Road1"
                if (Analysis.hit(ss, cP2StartA2, hitRoad, 4.pt, 0.55f)) hitPart++ // , "p2Road2"
                if (Analysis.hit(ss, cP2StartA3, hitRoad, 4.pt, 0.45f)) hitPart++ // , "p2Road3"
                if (Analysis.hit(ss, cP2StartA4, hitRoad, 4.pt, 0.45f)) hitPart++ //  "p2Road4"
                if (!Analysis.hit(ss, cP2StartA5, hitRoad, 4.pt, 0.1f)) hitPart++ // , "p2Road5"
                if (!Analysis.hit(ss, cP2StartA7, hitRoad, 4.pt, 0.1f)) hitPart++ // , "p2Road5"
                if (!Analysis.hit(ss, cP2StartA8, hitRoad, 4.pt, 0.1f)) hitPart++ // , "p2Road5"
                if (!Analysis.hit(ss, cP2StartA9, hitRoad, 4.pt, 0.1f)) hitPart++ // , "p2Road5"
                if (hitPart >= 8) {
                    // 这个判断很准确 新地图也准确。 确实准确有用
                    printLog { "analystFB 节点.P2起点 第1节点" }
                    anaLastSMapFBFeature1Ts = System.currentTimeMillis()
                }
            }
        }
        if (System.currentTimeMillis() - lastMapRightNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapBottomNotArrivedTs > 2000
            && !Analysis.hit(ss, cP2StartB1, hitRoad, 4.pt, 0.1f) // 0
            && !Analysis.hit(ss, cP2StartB2, hitRoad, 4.pt, 0.1f) // 0
            && !Analysis.hit(ss, cP2StartBX, hitRoad, 4.pt, 0.1f)) {// 1层误判。上船后左下角.
            var hitPart = 0
            if (Analysis.hit(ss, cP2StartB3, hitRoad, 4.pt, 0.6f)) hitPart++ // , "p2Road1"
            if (Analysis.hit(ss, cP2StartB4, hitRoad, 4.pt, 0.6f)) hitPart++ // , "p2Road2"
            if (Analysis.hit(ss, cP2StartB5, hitRoad, 4.pt, 0.5f)) hitPart++ // , "p2Road3"
            if (Analysis.hit(ss, cP2StartB6, hitRoad, 4.pt, 0.5f)) hitPart++ //  "p2Road4"
            if (hitPart >= 4) {
                printLog { "analystFB 节点.P2起点 第3节点" } // 会在最终终点误判
                anaLastSMapFBFeature1Ts = System.currentTimeMillis()
            }
        }

        if (System.currentTimeMillis() - lastMapRightNotArrivedTs > 2000
            && System.currentTimeMillis() - lastMapBottomNotArrivedTs > 2000
            && Analysis.hit(ss, cP2StartC1, hitRoad, 4.pt, 0.7f, ) // [cP2StartC1] 0.90909094 hitCount 60 total 66
            && !Analysis.hit(ss, cP2StartC2, hitRoad, 4.pt, 0.3f, ) // [cP2StartC2] 0.1388889 hitCount 5 total 36
            && Analysis.hit(ss, cP2StartC3, hitRoad, 4.pt, 0.7f, ) // [cP2StartC3] 0.8888889 hitCount 32 total 36
            && !Analysis.hit(ss, cP2StartC4, hitRoad, 4.pt, 0.3f, ) // [cP2StartC4] 0.0 hitCount 0 total 36
            && !Analysis.hit(ss, cP2StartC5, hitRoad, 4.pt, 0.3f, ) // [cP2StartC5] 0.0 hitCount 0 total 48
            ) {
            printLog { "analystFB 节点.P2起点 第2节点" }
            anaLastSMapFBFeature1Ts = System.currentTimeMillis()
        }
    }



    if (System.currentTimeMillis() - anaLastPageSceneEnterTs > 8000
        && System.currentTimeMillis() - xLastUnknownTs > 8000
        && System.currentTimeMillis() - anaLastHasEnemyNameTs > 4000
        && System.currentTimeMillis() - lastEliteEnemyBossTs > 4000
        && blockBlueDoor != null
        && blockBlacksmith != null
        && !bsSceneSettle && !bsSceneSettleBlacksmith
        && simpleAssertSceneCompleted(blockBlueDoor, blockBlacksmith)) {
        printLog { "analyst scene completed count $anaCounterSceneSettle" }
        anaCounterSceneSettle++
        if (anaCounterSceneSettle > 0) {
            printLog { "analyst scene completed" }
            anaMarkSceneSettle()
        }
    }
    if (System.currentTimeMillis() - lastBlueDoorTs > 3000
        && System.currentTimeMillis() - lastBlacksmithTs > 3000) {
        anaCounterSceneSettle = 0
    }
}

fun analysisMapFBIsStartPoint(ss: Bitmap): Boolean {
//    printLog { "analysisMapFBIsStartPoint analystFB $mapSimpleStatus" }
    if (System.currentTimeMillis() - lastMapTopNotArrivedTs > 2000
        && System.currentTimeMillis() - lastMapBottomNotArrivedTs < 1000
        && System.currentTimeMillis() - lastMapLeftNotArrivedTs > 2000
        && System.currentTimeMillis() - lastMapRightNotArrivedTs > 2000
        && lastMapTopP == null
        && (lastMapBottomP ?: -1f) in 0.53 .. 0.66
        && lastMapBottomC in 14 .. 23
        && lastMapLeftP == null
        && lastMapRightP == null
        && blockBlueDoor == null && blockBlacksmith == null
        && System.currentTimeMillis() - anaLastHasEnemyBossNameTs > 16000
//            && System.currentTimeMillis() - xLastUnknownTs < 6000
    ) {
        //  [上,1748696100s,c:0][下,0s,0.6286054,c:19][左,1748696100s,c:0][右,1748696100s,c:0]][Road,c:10]
        // [上,1748651489s,c:0][下,0s,0.5682674,c:18][左,1748651489s,c:0][右,1748651489s,c:0]][Road,c:10]
        // [上,77s,c:0][下,0s,0.6293488,c:19][左,170s,c:0][右,77s,c:0]][Road,c:11]
        // [上,1748651534s,c:0][下,0s,0.5682674,c:18][左,1748651534s,c:0][右,1748651534s,c:0]][Road,c:11]
        // [上,1748858811s,c:0][下,0s,0.6293488,c:19][左,1748858811s,c:0][右,1748858811s,c:0]][Road,c:11]
        printLog { "analystFB 节点.风暴路线 是起点A" }
        anaLastSMapFBFeature13Ts = System.currentTimeMillis()
        return true
    } else if (
        System.currentTimeMillis() - lastMapTopNotArrivedTs > 5000
        && System.currentTimeMillis() - lastMapLeftNotArrivedTs > 5000
        && System.currentTimeMillis() - lastMapRightNotArrivedTs > 5000
        && blockBlueDoor == null && blockBlacksmith == null
        && System.currentTimeMillis() - lastEliteEnemyBossTs > 16000
        && System.currentTimeMillis() - anaLastHasEnemyBossNameTs > 16000
        && Analysis.hit(ss, cP1StartPoint1, hitRoad, 4.pt, 0.8f)) {
        printLog { "analystFB 节点.风暴路线 是起点B" }
        anaLastSMapFBFeature13Ts = System.currentTimeMillis()
        return true
    }
    return false
}

private fun simpleAssertSceneCompleted(blueDoor: Block?, blacksmith: Block?): Boolean {
    if (blueDoor == null) return false
    if (blacksmith == null) return false
    val edge = 80.pt
    return (abs(blueDoor.x - blacksmith.x) < edge && abs(blueDoor.y - blacksmith.y) < edge)

}
private fun angleSpecialAdjust(angle: Int): Int {
    if (angle in 200 .. 340) {
        if (System.currentTimeMillis() - xLastAtkTs > 5000
            && System.currentTimeMillis() - lastEliteEnemyBossTs > 5000
            && System.currentTimeMillis() - lastEliteEnemyTs > 5000
            && System.currentTimeMillis() - anaLastSMapFBFeature1Ts > 20000
            && System.currentTimeMillis() - anaLastSMapFBFeature7Ts > 20000
            && System.currentTimeMillis() - xLastSceneEnterTs > 30000
            && System.currentTimeMillis() - xLastPetBlacksmithTs > 20000
            && System.currentTimeMillis() - xLastUnknownTs < 20000) {
            printLog { "analystFB angleSpecialAdjust $angle to 90" }
            return 90
        }
    }
    return angle
}
